/**
 * @see 该页面是状态通用界面，此界面从不同的业务进入，需要带入参数进入。进入时根据业务类型来做不同的处理。
 * @see 需要把以下参数放入一个Objet里，再转换成字符串传入
 * @see 接受参数主要有：
 * @param {*title 标题} String
 * @param {*info 描述} String
 * @param {*btnConfirm 是否显示确认按钮} Boolen
 * @param {*type 状态类型（success warn）} String
 * @param {*btnOthers 按钮数组（  {text, url} ）} ObjectArray
 * @example /pages/workService/components/status/index?data=***
 */
import { getPageAdPicList, handleUrlParams } from "../../../../utils/util"

Page({
	data: {
		type: "", // success warn
		title: "", // 打印委托
		info: "",
		btnConfirm: true,
		btnOthers: [],
		schoolCode: "",
		// 广告图片
		adImageItem: {
			imgAddress: "",
			placementLocation: "",
			jumpType: "",
			jumpAddress: "",
			appid: "",
		},
	},
	onLoad(option) {
		// 获取广告图片
		getPageAdPicList("workService:status").then((list) => {
			if (list && list.length > 0) {
				this.setData({
					adImageItem: list[0],
				})
			}
		})

		const schoolInfo = wx.getStorageSync("schoolConfig")
		if (schoolInfo) {
			this.setData({
				schoolCode: schoolInfo.schoolCode.toString(),
			})
		}
		try {
			const data = JSON.parse(decodeURIComponent(option.data))
			const { type, title, info, btnConfirm, btnOthers } = data
			this.setData({
				type,
				title,
				info,
				btnConfirm,
				btnOthers,
			})
		} catch (error) {
			console.log(error)
		}
		//计时器，等待两秒后跳转到海报页面
		//排除重庆医科大学、中南财经大学
		setTimeout(() => {
			this.jumpToPosterPage()
		}, 2000)
	},
	confirm() {
		if (this.data.adImageItem.imgAddress) {
			this.jumpToPosterPage()
		} else {
			// 回到首页
			wx.reLaunch({
				url: "/pages/home/index",
			})
		}
	},
	//跳转到海报页面
	jumpToPosterPage() {
		if (!this.data.adImageItem.imgAddress) {
			return
		}
		let jumpType = ""
		switch (this.data.adImageItem.jumpType) {
			case "Applet":
				jumpType = "jumpMiniProgram"
				break
			case "Instation":
				jumpType = "jumpPage"
				break
			case "H5":
				jumpType = "jumpH5"
				break
		}
		//跳转到海报页面
		wx.navigateTo({
			url: handleUrlParams("/pages/posterPage/index", {
				path: encodeURIComponent(this.data.adImageItem.imgAddress),
				title: encodeURIComponent("领福利"),
				jumpType: jumpType,
				appId: this.data.adImageItem.appid,
				jumpPath: encodeURIComponent(this.data.adImageItem.jumpAddress),
			}),
		})
	},
})
